{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyNxMtdWTBzmUVRZJLMBfmmo",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/stanfordnmbl/opencap-processing/blob/main/batchDownload.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Batch download\n",
        "\n",
        "**Aim**\n",
        "\n",
        "With this jupyter notebook, you can download data collected using [OpenCap](https://www.opencap.ai/). Note that you can also use the python script [batchDownload.py](https://github.com/stanfordnmbl/opencap-processing/blob/main/batchDownload.py) for this purpose.\n",
        "\n",
        "**Users inputs**\n",
        "\n",
        "You will need to authenticate using the credentials you used when collecting data with the [web application](https://app.opencap.ai/). You will also need to provide the IDs of the sessions you would like to download (see Download data below)."
      ],
      "metadata": {
        "id": "ozEyD2dWIsuu"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Setup conda and install packages\n",
        "\n",
        "In the following cell, you will use [condacolab](https://github.com/conda-incubator/condacolab) to install [Miniconda](https://docs.conda.io/en/latest/miniconda.html). You will also install several packages needed for downloading your data."
      ],
      "metadata": {
        "id": "u855yu106dry"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 45,
      "metadata": {
        "id": "1hd2tkuEEWUf",
        "outputId": "464f1053-aa44-46ab-dd5e-bebc51af0440",
        "colab": {
          "base_uri": "https://localhost:8080/"
        }
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0m✨🍰✨ Everything looks OK!\n",
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: python-decouple in /usr/local/lib/python3.7/site-packages (3.6)\n",
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0mLooking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: maskpass==0.3.6 in /usr/local/lib/python3.7/site-packages (0.3.6)\n",
            "Requirement already satisfied: pynput in /usr/local/lib/python3.7/site-packages (from maskpass==0.3.6) (1.7.6)\n",
            "Requirement already satisfied: evdev>=1.3 in /usr/local/lib/python3.7/site-packages (from pynput->maskpass==0.3.6) (1.6.0)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from pynput->maskpass==0.3.6) (1.16.0)\n",
            "Requirement already satisfied: python-xlib>=0.17 in /usr/local/lib/python3.7/site-packages (from pynput->maskpass==0.3.6) (0.31)\n",
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0mLooking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (2.28.1)\n",
            "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests) (1.26.11)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests) (2022.6.15)\n",
            "Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.7/site-packages (from requests) (2.1.1)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests) (3.3)\n",
            "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
            "\u001b[0m"
          ]
        }
      ],
      "source": [
        "!pip install -q condacolab\n",
        "import condacolab\n",
        "condacolab.install()\n",
        "!pip install python-decouple\n",
        "!pip install maskpass==0.3.6\n",
        "!pip install requests\n",
        "!conda install -c opensim-org opensim=4.4.1=py310np121"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Clone repository\n",
        "\n",
        "The code required to download your data is part of [opencap-processing](https://github.com/stanfordnmbl/opencap-processing). You will here clone the repository (or update it) and add it to the path."
      ],
      "metadata": {
        "id": "CO40IXqc642z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "if os.path.exists(os.path.join(os.getcwd(), 'opencap-processing')):\n",
        "  %cd opencap-processing\n",
        "  !git pull\n",
        "  %cd ..\n",
        "else:\n",
        "  !git clone https://github.com/stanfordnmbl/opencap-processing.git\n",
        "import sys\n",
        "sys.path.append('./opencap-processing')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "CtNBj5OWFe0T",
        "outputId": "a6131cda-f165-4508-988d-e570bb6776ed"
      },
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/opencap-processing\n",
            "Already up to date.\n",
            "/content\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Download data\n",
        "\n",
        "Add the IDs of the sessions you would like to download to the variable `sessionList` in the code cell below, you can list as many session IDs as you like. The session ID is the 36-character string at the end of the session url. For example, the session ID for https://app.opencap.ai/session/7272a71a-e70a-4794-a253-39e11cb7542c is '7272a71a-e70a-4794-a253-39e11cb7542c'. You can find your sessions at https://app.opencap.ai/sessions. You will be prompted to enter your username and password. The data will be downloaded in your Downloads folder."
      ],
      "metadata": {
        "id": "UaO5mayq-NaE"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from utils import download_session\n",
        "import shutil\n",
        "from google.colab import files  \n",
        "\n",
        "# Enter the session IDs you would like to download.\n",
        "sessionList = ['7272a71a-e70a-4794-a253-39e11cb7542c',\n",
        "                'abe79267-646f-436b-a19e-a9e1d8f32807']\n",
        "\n",
        "downloadPath = os.path.join(os.getcwd(), 'Data')\n",
        "for session_id in sessionList:\n",
        "  # If you only want markers and kinematics, downloadVideos=False will be faster.\n",
        "  download_session(session_id,sessionBasePath=downloadPath,downloadVideos=True)\n",
        "  pathData = os.path.join(downloadPath, 'OpenCapData_' + session_id)\n",
        "  shutil.make_archive(pathData, 'zip', pathData)\n",
        "  shutil.rmtree(pathData)\n",
        "  pathZip = pathData + '.zip'\n",
        "  files.download(pathZip)"
      ],
      "metadata": {
        "id": "euExyeyQJK_O"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
